import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import path from 'path';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from '@vant/auto-import-resolver';
import viteCompression from 'vite-plugin-compression'
import pxtorem from 'postcss-pxtorem';

export default defineConfig({
  server: {
    host: '0.0.0.0', // 允许外部访问
    port: 5173, // 可选，指定端口
    proxy: {
      '/apiLocal': {
        target: 'http://192.168.0.218:8188/',
        changeOrigin: true,
        secure: false, // 是否验证 SSL 证书
        rewrite: (path) => path.replace(/^\/apiLocal/, '')
      },
      '/apiTest': {
        target: 'https://zttest.crm1001.com/',
        ws: true,
        changeOrigin: true,
        secure: false, // 是否验证 SSL 证书
        rewrite: (path) => path.replace(/^\/apiTest/, '')
      },
      '/apiPro': {
        target: 'https://zt.crm1001.com/',
        ws: true,
        changeOrigin: true,
        logLevel: 'debug',
        secure: false, // 是否验证 SSL 证书
        rewrite: (path) => path.replace(/^\/apiPro/, '')
      }
    }
  },
  plugins: [
    vue(),
    AutoImport({
      resolvers: [VantResolver()]
    }),
    Components({
      resolvers: [VantResolver()]
    }),
    // viteCompression()
  ],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src')
    }
  },
  css: {
    postcss: {
      plugins: [
        pxtorem({
          // vant默认是37.5
          rootValue(res) {
            return res.file.indexOf("vant") !== -1 ? 37.5 : 200; // 设计图的1/10
          },
          propList: ['*'], // 需要转换的属性，默认转换所有属性
          selectorBlackList: [], // CSS选择器黑名单，防止部分选择器被转换
          exclude: /\/node_modules\//i, // 忽略包文件转换rem
        })
      ]
    },
    preprocessorOptions: {
      scss: {
        additionalData: `@use "@/styles/variables.scss" as *;`
      }
    }
  }
});
